﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Threading.Tasks;
using System.Data.SqlClient;
using DAL;

namespace BLL
{
    public class ForBookUpdate
    {
        public SqlDataAdapter SeeBookClass()
        {
            //查询基本类
            string bClass = @"select * from [dbo].[BookClass];";
            SqlDataAdapter adapter = new SqlDataAdapter(bClass, DBHelper.Connection);
            return adapter;
        }
        //查询具体类
        public SqlDataAdapter SeeBookType(int ClassID = -10)
        {
            string type;
            if (ClassID != -10)
            {
                //SQL语句
                type = string.Format(@"select [BookTypeID],[BookType] 
                        from [dbo].[BookType] where ClassID = {0}"
                        , ClassID);
            }
            else
            {
                type = "select [BookTypeID],[BookType] from [dbo].[BookType]";
            }
            SqlDataAdapter adapter = new SqlDataAdapter(type, DBHelper.Connection);
            return adapter;
        }
        //修改图书状态
        public int UpdateBooksState(int StateID,int BookID)
        {
            StringBuilder sb = new StringBuilder();
            //修改图书状态
            sb.AppendFormat(@"update Books set StateID = {0} where BookID = {1}", StateID, BookID);
            //执行
            int iRet = DBHelper.ExecuteNonQuery(sb.ToString());
            return iRet;
        }
        //修改图书信息
        public int UpdateBookInfo(string BookName,int BookTypeID,int ClassID,string Press,string Author,int Words,decimal Price,int ID)
        {
            StringBuilder sb = new StringBuilder();
            sb.AppendFormat(@"update Book set BookName = '{0}',BookTypeID = {1},ClassID = {2},
                                        Press = '{3}',Author = '{4}',Words = '{5}',Time = GETDATE(),Price = {6}
                                        where ID = {7}", BookName, BookTypeID, ClassID, Press, Author, Words, Price, ID);
            //执行更新
            int iRet = DBHelper.ExecuteNonQuery(sb.ToString());
            return iRet;
        }
        public SqlDataReader SeeBookInfoByBookID(int BookID)
        {
            StringBuilder sb = new StringBuilder();
            //查询语句
            sb.AppendFormat(@"select ID,StateID from Books where BookID = {0};", BookID);
            //执行
            SqlDataReader reader = DBHelper.ExecuteReader(sb.ToString());
            return reader;
        }
        //查找图书具体信息
        public SqlDataReader SeeBookInfoByID(int ID)
        {
            StringBuilder sb = new StringBuilder();
            //查询语句
            sb.AppendFormat(@"select BookName,Press,Author,Words,Price,ClassID,BookTypeID 
                                from Book where ID = {0};", ID);

            //执行
            SqlDataReader reader = DBHelper.ExecuteReader(sb.ToString());
            return reader;
        }
    }
}
